//
//  RHSolarkSettingModel.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/5/18.
//

#import "RHSolarkSettingModel.h"

@implementation RHSolarkSettingModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"num":@"bitWidth"};
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"rows" : [RHSolarkSettingModel class],
             @"items" : [RHSolarkSettingModel class],
             @"selectList":[DevSettingOptionModel class]};
 }

- (void)readDefult:(NSDictionary *)dataDic
{
    NSMutableDictionary *dic = dataDic.mutableCopy;
  
    kWeakSelf;
    [self.rows enumerateObjectsUsingBlock:^(RHSolarkSettingModel * _Nonnull rowModel, NSUInteger row_idx, BOOL * _Nonnull stop) {
        //时间校准
        if ([rowModel.type isEqualToString:@"date"]) {
            rowModel.content = [NSString stringWithFormat:@"20%02ld/%02ld/%02ld %02ld:%02ld:%02ld",[dic[@"Inv3_RTCYear"] integerValue],[dic[@"Inv3_RTCMonth"] integerValue],[dic[@"Inv3_RTCDay"] integerValue],[dic[@"Inv3_RTCHour"] integerValue],[dic[@"Inv3_RTCMinute"] integerValue],[dic[@"Inv3_RTCSecond"] integerValue]];
            return;
        }
        //充放电时间设置
        if ([rowModel.type isEqualToString:@"time"]) {
            [rowModel.items enumerateObjectsUsingBlock:^(RHSolarkSettingModel * _Nonnull itemModel, NSUInteger item_idx, BOOL * _Nonnull stop) {
                NSLog(@"---->key:%@   value:%@",itemModel.key,dic[itemModel.key]);
               
                if ([itemModel.type isEqualToString:@"select"]) {
                    //下标值
                    for (DevSettingOptionModel *select_model in itemModel.selectList) {
                        //有时才处理
                        if(([dic[itemModel.key] integerValue] == select_model.textValue)&&dic[itemModel.key]){
                            itemModel.enumText = select_model.text;
                            itemModel.content = [NSString stringWithFormat:@"%ld",select_model.textValue];
                        }
                    }
                }
                //充放电开始时间和结束时间
                if ([itemModel.type isEqualToString:@"date"] && [dic[itemModel.key] length] != 0) {
                    //结束时间的key,json文件里没有配
                    //充电开始 结束时间
                    NSString *chg_startTimeKey_H = [NSString stringWithFormat:@"TOU_Time_%ld_Hour",row_idx+1];
                    NSString *chg_startTimeKey_M = [NSString stringWithFormat:@"TOU_Time_%ld_Minute",row_idx+1];
                    
                    NSString *chg_endTimeKey_H = [NSString stringWithFormat:@"TOU_Time_%ld_Hour",row_idx+1+1];
                    NSString *chg_endTimeKey_M = [NSString stringWithFormat:@"TOU_Time_%ld_Minute",row_idx+1+1];
                    if (row_idx == weakSelf.rows.count-1) {
                        chg_endTimeKey_H = @"TOU_Time_1_Hour";
                        chg_endTimeKey_M = @"TOU_Time_1_Minute";
                    }
                    
                    //放电开始 结束时间
                    NSInteger start_hour = 0;
                    NSInteger start_minute = 0;
                    NSInteger end_hour = 0;
                    NSInteger end_minute = 0;
                    //充电开始时间 小时
                    if ([itemModel.key isEqualToString:chg_startTimeKey_H]) {
                        start_hour = [dic[chg_startTimeKey_H] integerValue];
                        start_minute = [dic[chg_startTimeKey_M] integerValue];
                        itemModel.content = [NSString stringWithFormat:@"%02ld:%02ld",start_hour,start_minute];
                        
                    }
                    //充电线束时间 小时
                    if ([itemModel.key isEqualToString:chg_endTimeKey_H]) {
                        end_hour = [dic[chg_endTimeKey_H] integerValue];
                        end_minute = [dic[chg_endTimeKey_M] integerValue];
                        itemModel.content = [NSString stringWithFormat:@"%02ld:%02ld",end_hour,end_minute];
                    }
                    
                  
                }
                if ([itemModel.type isEqualToString:@"switch"]){
                    itemModel.content = ValidNullData(dic[itemModel.key]);
                }
                if ([itemModel.type isEqualToString:@"number"]){
                    itemModel.content = ValidNullData(dic[itemModel.key]);
                }
               
               
            }];
            return;
        }
        
        
        
        //开关和数值输入
        NSLog(@"key:%@   value:%@",rowModel.key,dic[rowModel.key]);
        rowModel.content = ValidNullData(dic[rowModel.key]);
        
        //选择
        if ([rowModel.type isEqualToString:@"select"]) {
            //下标值
            for (DevSettingOptionModel *select_model in rowModel.selectList) {
                if([dic[rowModel.key] integerValue] == select_model.textValue){
                    rowModel.enumText = select_model.text;
                }
            }
        }
    }];
}
@end
